package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.business.service.CourseUnitGrammarStudyService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitGrammarStudy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (course_unit_grammar_study)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-grammar-study")
@Slf4j
public class CourseUnitGrammarStudyController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitGrammarStudyService courseUnitGrammarStudyService;

    /**
     * 保存语法
     *
     * @param request
     * @param courseUnitGrammarStudy
     * @return
     */
    @PostMapping("/save/course-unit-grammar-study")
    public Result saveCourseUnitGrammarStudy(HttpServletRequest request, @RequestBody CourseUnitGrammarStudy courseUnitGrammarStudy) {
        log.info("CourseUnitGrammarStudyController saveCourseUnitGrammarStudy ,参数:{}", courseUnitGrammarStudy);
        courseUnitGrammarStudyService.save(courseUnitGrammarStudy);
        return Result.success();
    }

    /**
     * 修改语法
     *
     * @param request
     * @param courseUnitGrammarStudy
     * @return
     */
    @PutMapping("/modify/course-unit-grammar-study")
    public Result modifyCourseUnitGrammarStudy(HttpServletRequest request, @RequestBody CourseUnitGrammarStudy courseUnitGrammarStudy) {
        log.info("CourseUnitGrammarStudyController modifyCourseUnitGrammarStudy ,参数:{}", courseUnitGrammarStudy);
        courseUnitGrammarStudyService.updateById(courseUnitGrammarStudy);
        return Result.success();
    }

    /**
     * 通过单元id,获取语法列表分页
     *
     * @param request
     * @param courseUnitId
     * @param pageQuery
     * @return
     */
    @PostMapping("/query/course-unit-grammar-study-by-course-unit-id-page/{courseUnitId}")
    public Result queryCourseUnitGrammarStudyByCourseUnitIdPage(HttpServletRequest request, @PathVariable("courseUnitId") Integer courseUnitId, @RequestBody PageQuery pageQuery) {
        log.info("CourseUnitGrammarStudyController queryCourseUnitGrammarStudyByCourseUnitIdPage ,参数courseUnitId:{},pageQuery:{}", courseUnitId, pageQuery);
        IPage<CourseUnitGrammarStudy> courseUnitGrammarStudyIPage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<CourseUnitGrammarStudy> courseUnitGrammarStudyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitGrammarStudyLambdaQueryWrapper.eq(CourseUnitGrammarStudy::getCourseUnitId, courseUnitId);
        return Result.success(courseUnitGrammarStudyService.page(courseUnitGrammarStudyIPage, courseUnitGrammarStudyLambdaQueryWrapper));
    }
}
